using System;
using System.Threading;
using System.Globalization;
using System.Configuration;

using ScenarioFramework.WorkflowEngine.DataModel.WorkflowApplicationsData;

using log4net;
using log4net.Config;
using ScenarioFramework.Modules.FormHelper;
using ScenarioFramework.Modules.DM.DataBase;
using ScenarioFramework.Modules;

namespace ScenarioFramework.Portal
{
	public class Global : System.Web.HttpApplication
	{
		#region Application Object - Static
		public static FormsHelper FormsHelper = new FormsHelper();

		public static bOSdotNET.DB.Counter idOPCounter = null;

		public static bOSdotNET.DB.Counter idLotCounter = null;

		#endregion

		// variabile per ottenere la risposta dal motore 
		private ScenarioFramework.WAPI.Response wapiResponse = new ScenarioFramework.WAPI.Response();

		private System.ComponentModel.IContainer components = null;

		public static ScenarioFramework.Modules.DM.DataBase.DocumentManagerDBHelper dmDBHelper = null;

		private ILog Logger = LogManager.GetLogger(typeof(Global));

		public Global()
		{
			InitializeComponent();
		}	

		protected void Application_Start(Object sender, EventArgs e)
		{
            XmlConfigurator.Configure();

			Logger.Info("Inizializing Scenario Engine");

			//Istanza del motore di Workflow
			ScenarioFramework.WAPI.Session.InitSession(ref wapiResponse,"SFPortal",Global.TraceLevel);

			if ( wapiResponse.iCode != 0 )
			{
				//SiteError.Add ( wapiResponse.sMsg );
				return;
			}

			#region FormHelper
			try
			{
				FormsHelper.Setup();
			}
			catch ( ScenarioFramework.Modules.FormHelper.Exception.FormHelperException)
			{
				//SiteError.Add (err.Message );
			}
			#endregion

			#region Read ApplicationsFiles and execute all exe Applications
			string sXML = ScenarioFramework.WAPI.Application.GetApplications(ref wapiResponse );

			if ( wapiResponse.iCode != 0 ) 
			{
				return;
			}

			XMLApplications xmlApplications = new XMLApplications();
			xmlApplications.Parse (sXML);

			Application["ApplicationsFile"] = xmlApplications;

			ScenarioFramework.WAPI.Application.RunAllApplications (xmlApplications);
			#endregion

			dmDBHelper = new DocumentManagerDBHelper();
		}

		protected void Session_Start(Object sender, EventArgs e)
		{
           
		}

		protected void Application_BeginRequest(Object sender, EventArgs e)
		{
			Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");

			Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
		}

		protected void Application_EndRequest(Object sender, EventArgs e)
		{
        }

		protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
		{
		}

	
		protected void Application_Error(Object sender, EventArgs e)
		{
		}

		protected void Session_End(Object sender, EventArgs e)
		{
		}

		protected void Application_End(Object sender, EventArgs e)
		{
			ScenarioFramework.WAPI.Session.EndSession(ref wapiResponse);
		}

		private static int TraceLevel 
		{
			get 
			{
				string str = ConfigurationSettings.AppSettings["TraceLevel"];
				if (str==null || str.Length <=0)
					return 7;
				else
					return Int32.Parse (str);
			}
		}

		
		#region Codice generato da Progettazione Web Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{    
			this.components = new System.ComponentModel.Container();
		}
		#endregion

	}
}

